package com.jpx.jxcbiz.web;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.jpx.common.vo.CustomerGoodsListDto;
import com.jpx.common.vo.CustomerListDto;
import com.jpx.common.vo.ResultEntity;
import com.jpx.jxcbiz.entity.TCustomerReturnList;
import com.jpx.jxcbiz.entity.TCustomerReturnListGoods;
import com.jpx.jxcbiz.entity.TUser;
import com.jpx.jxcbiz.service.ITCustomerReturnListGoodsService;
import com.jpx.jxcbiz.service.ITCustomerReturnListService;
import com.jpx.jxcbiz.service.ITUserService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;


/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author Mht
 * @since 2020-02-24
 */

@RestController
@RequestMapping("/biz/tCustomerReturnList")
public class TCustomerReturnListController {

    @Autowired
    ITCustomerReturnListService itCustomerReturnListService;

    @Autowired
    ITUserService itUserService;

    @Autowired
    ITCustomerReturnListGoodsService itCustomerReturnListGoodsService;

    /**
     * 商品退货列表以及查询
     * @param tCustomerReturnList
     * @return
     */
    @RequestMapping("/getReturnList")
    public ResultEntity getRetureList(TCustomerReturnList tCustomerReturnList){
        Wrapper wrapper = new EntityWrapper();
        if(tCustomerReturnList.getBname()!=null && tCustomerReturnList.getBname()!=""){
            wrapper.eq("bname",tCustomerReturnList.getBname());
        }
        if(tCustomerReturnList.getReturnNumber()!=null && tCustomerReturnList.getCustomerReturnNumber()!=""){
            wrapper.eq("customer_return_number",tCustomerReturnList.getCustomerReturnNumber());
        }
        if(tCustomerReturnList.getState()!=null){
            wrapper.eq("state",tCustomerReturnList.getState());
        }
        if(tCustomerReturnList.getStartTime()!=null && tCustomerReturnList.getEndTime()!=null){
            wrapper.between("customer_return_date",tCustomerReturnList.getStartTime(),tCustomerReturnList.getEndTime());
        }
        return ResultEntity.ok(itCustomerReturnListService.getGoodsRetureList(tCustomerReturnList));
    }

    /**
     * 根据商品退货的Id查询出具体商品
     * @param tCustomerReturnList
     * @return
     */
    @RequestMapping("/getReturnGoodsById")
    public ResultEntity getSaleById(TCustomerReturnList tCustomerReturnList){
        return ResultEntity.ok(itCustomerReturnListService.getReturnGoodsById(tCustomerReturnList.getId()));
    }

    /**
     * 根据退货订单Id删除订单以及具体商品
     * @param id
     * @return
     */
    @RequestMapping("/deleteReturnGoodsById")
    public ResultEntity deleteReturnGoodsById(Integer id){
        //根据退货Id删除退货具体商品
        itCustomerReturnListService.deleteReturnGoodsById(id);
        //根据退货Id删除退货订单
        itCustomerReturnListService.deleteReturnById(id);
        return ResultEntity.ok();
    }

    /**
     * 退货
     * @return
     */
    @RequestMapping("/returnGoods")
    public ResultEntity returnGoods(@RequestBody CustomerListDto customerListDto){
        //根据用户名查找用户ID
        String username = customerListDto.getUserName();
        Wrapper<TUser> wrapper = new EntityWrapper();
        wrapper.eq("user_name",username);
        TUser user = itUserService.selectOne(wrapper);

        //添加退货表
        TCustomerReturnList returnList = new TCustomerReturnList();
        BeanUtils.copyProperties(customerListDto,returnList);
        returnList.setUserId(user.getId());
        itCustomerReturnListService.insertReturnList(returnList);

        List<CustomerGoodsListDto> list = customerListDto.getList();

        //遍历退货信息 添加到表
        for (CustomerGoodsListDto listGoods : list ) {
            TCustomerReturnListGoods returnListGoods = new TCustomerReturnListGoods();
            returnListGoods.setGoodsId(listGoods.getId());
            returnListGoods.setCustomerReturnListId(returnList.getId());
            returnListGoods.setCode(listGoods.getCode());
            returnListGoods.setModel(listGoods.getModel());
            returnListGoods.setName(listGoods.getName());
            returnListGoods.setNum(listGoods.getNum());
            returnListGoods.setPrice(listGoods.getSellingPrice());
            returnListGoods.setTotal(listGoods.getNum()*listGoods.getSellingPrice());
            returnListGoods.setTypeId(listGoods.getTypeId());
            returnListGoods.setUnit(listGoods.getUnit());
            //添加
            itCustomerReturnListGoodsService.insert(returnListGoods);
        }

        return ResultEntity.ok();
    }
}
